package com.cwt_taxi_xa.custom_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.cwt_taxi_xa.R;


/**
 * Created by ZQX on 2019/7/13 11:09
 */
public class ToggleView extends View {

    int widthSize;
    int heightSize;
    int rsSrcId;
    int rsIndicatorId;

    boolean on = false;

    public ToggleView(Context context) {
        this(context,null);
    }

    public ToggleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public ToggleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        typedYView(context,attrs, defStyleAttr);
    }

    private void typedYView(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.toggle_styleable);
        rsIndicatorId = a.getResourceId(R.styleable.toggle_styleable_viewIndicator, 0);
        rsSrcId = a.getResourceId(R.styleable.toggle_styleable_viewToggleSrc, 0);
        a.recycle();//必须回收
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        //setMeasuredDimension(widthMeasureSpec, height);
        int w = 0;
        int h = 0;
        w = Math.max(w, getSuggestedMinimumWidth());
        h = Math.max(h, getSuggestedMinimumHeight());

        widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);
        heightSize = resolveSizeAndState(h, heightMeasureSpec, 0);

        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), rsSrcId);
        Bitmap bitmapIndicator = BitmapFactory.decodeResource(getResources(), rsIndicatorId);
        Log.d("ToggleView","bitmapIndicator  width:" + bitmapIndicator.getWidth() + " height:" + bitmapIndicator.getHeight());
        Log.d("ToggleView","bitmap  width:" + bitmap.getWidth() + " height:" + bitmap.getHeight());
        if(bitmapIndicator.getHeight() == 6){
            canvas.drawBitmap(bitmapIndicator,(widthSize - bitmapIndicator.getWidth())/2,20,null);
            canvas.drawBitmap(bitmap,(widthSize - bitmap.getWidth())/2,45,null);
        }else{
            canvas.drawBitmap(bitmapIndicator,(widthSize - bitmapIndicator.getWidth())/2,9,null);
            canvas.drawBitmap(bitmap,(widthSize - bitmap.getWidth())/2,bitmapIndicator.getHeight(),null);
        }
    }

    public void setSrcAndIndicator(int rsSrcId,int rsIndicatorId){
        this.rsSrcId = rsSrcId;
        this.rsIndicatorId = rsIndicatorId;
        invalidate();
    }

    private void setIndicator(int rsIndicatorId){
        this.rsIndicatorId = rsIndicatorId;
        invalidate();
    }
}
